<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <![endif]-->
</head>
<body>
<script type="text/javascript">

    function getTid() {
        let reg = new RegExp('tid=([^]*)');
        let r = window.location.search.substr(1).match(reg);
        return r[1];
    }

    function getUndoneUserList() {
        $.ajax({
            url:"GetUndoneUserListServlet",
            type:"post",
            data:{
                tid: getTid(),
            },
            success:function (data) {
                $("#panel-body").text(" ");
                for(let i=0;i < data.length;i++){
                    $("#panel-body").append(data[i].username+",");
                }
            }
        })
    }

    function showDetail() {
        $.ajax({
            url:"GetTaskDetailServlet",
            type: "post",
            data: {
                "tid":getTid()
            },
            success:function (data) {
                $("#title").prepend(data.title);
                let createTime = new Date(data.createTime);
                $("#createTime").append(createTime.getFullYear()+"-"+createTime.getMonth()+"-"+createTime.getDate()+" "+createTime.getHours()+":"+createTime.getMinutes());
                $("#deadline").append(data.deadline);
            }
        })
    }

    window.onload = function () {
        showDetail();
        getUndoneUserList();
    }
    $(function () {
        $("#submit").click(function () {
            let uid = $("#uid").val();
            let username = $("#username").val();
            if(uid.length <= 0|| username.length <= 0){
                $("#alert2").css('display','block');
                $("#alert2").text("请补全学号或姓名");
            } else {
                $.ajax({
                    url:"SetStatusServlet",
                    type:"post",
                    data:{
                        uid:$("#uid").val(),
                        username:$("#username").val(),
                        tid:getTid()
                    },
                    success:function (data) {
                        if(data.flag){
                            $("#alert2").css('display','none');
                            $("#alert1").css('display','block');
                            $("#alert1").text(data.msg);
                            getUndoneUserList();
                        } else {
                            $("#alert1").css('display','none');
                            $("#alert2").css('display','block');
                            $("#alert2").text(data.msg);
                        }
                    }
                })
            }
        })
    })
</script>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">
            <!--生成汉堡按钮-->
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.html">Takser</a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a href="login.html">新建任务</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="about.html">关于</a></li>
            </ul>
        </div>
    </div>
</nav>

<div class="container">
    <div>
        <h3 id="title"><br><small id="createTime">创建时间：</small>&nbsp;&nbsp;&nbsp;<small id="deadline">截止日期：</small></h3>
    </div>

    <div id="alert1" class="alert alert-success" role="alert" style="display: none"></div>
    <div id="alert2" class="alert alert-danger" role="alert" style="display: none"></div>

    <form method="post">
        <div class="form-group">
            <label for="uid">学号</label>
            <input type="text" class="form-control" id="uid" name="uid" placeholder="请输学号后两位">
        </div>
        <div class="form-group">
            <label for="username">姓名</label>
            <input type="text" class="form-control" id="username" name="username" placeholder="请输入姓名">
        </div>
        <button type="button" id="submit" class="btn btn-default">提交</button>
    </form>

    <div class="panel panel-default" style="margin-top: 15px">
        <div class="panel-heading">
            <h3 class="panel-title">未完成名单</h3>
        </div>
        <div class="panel-body" id="panel-body">
        </div>
    </div>
</div>
</body>
</html>